package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class InfoPanel extends JPanel implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel dtm;
	private JTextArea inta;
	private JScrollPane jsp,jsp1;
	private JLabel player;
	Font font=new Font("微软雅黑",Font.BOLD,13);
	

	public InfoPanel() {
		setBackground(new Color(0xffffaa));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel top = new JPanel();
		top.setLayout(new GridBagLayout());
		top.setPreferredSize(new Dimension(230, 100));
		GridBagConstraints gbc = new GridBagConstraints();
		JLabel label = new JLabel(new ImageIcon(getClass().getResource("../images/p1.jpg")));
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(0, 0, 0, 0);
		top.add(label, gbc);
		player = new JLabel("玩家x");
		gbc.gridy = 1;
		top.add(player, gbc);
		add(top);

		JPanel middle = new JPanel();
		middle.setLayout(new BorderLayout());
		dtm = new DefaultTableModel(new Object[][] {}, new String[] { "昵称", "IP", "联机时间" });
		JTable table = new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		jsp = new JScrollPane(table);
		jsp.setPreferredSize(new Dimension(230, 150));
		middle.add(jsp);
		add(middle);

		JPanel down = new JPanel();
		down.setLayout(new BorderLayout());
		inta = new JTextArea();
		inta.setLineWrap(true);
		inta.setEnabled(false);
		inta.setFont(font);
		inta.setDisabledTextColor((new Color(0x008B00)));
		jsp1 = new JScrollPane(inta);
		down.setPreferredSize(new Dimension(230, 300));
		JPanel shuru = new JPanel();
		shuru.setLayout(new BorderLayout());
		JTextField tfshuru = new JTextField();
		shuru.add(tfshuru);

		JButton btshuru = new JButton("发送");
	
		
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日hh:mm:ss");
		String s = sdf.format(new Date());
		

		btshuru.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e){
				String message = s+"   "+SocketUtil.getUser().getName() + ":" + "\n"+ tfshuru.getText() + "\n";
				if(tfshuru.getText().trim().equals(""))return;
				inta.append(message);
				SocketUtil.sendMessage(message);
				tfshuru.setText("");
			}
			
		});
		
		tfshuru.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				int code=e.getKeyCode();
				
//			   JLabel =new JLabel(xinxi);
//			   JLabel xinxi=(JLabel)SocketUtil.getUser().getName();
		
				switch (code) {
				case KeyEvent.VK_ENTER:
					String message =s+"   "+ SocketUtil.getUser().getName()+ ":" +"\n" +tfshuru.getText() + "\n";
					if(tfshuru.getText().trim().equals(""))return;
					inta.append(message);
					SocketUtil.sendMessage(message);
					tfshuru.setText("");					
					break;
				}
				
				
			}
		});
		shuru.add(tfshuru);
		shuru.add(btshuru, BorderLayout.EAST);
		down.add(jsp1);
		down.add(shuru, BorderLayout.SOUTH);
		add(down);
		
		

	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof User) {
			User user = (User) arg;
			dtm.addRow(new Object[] { user.getName(), user.getIp(), user.getLogintime() });
			if (o.equals(user)) 
				player.setText(user.getName());
		} else if (arg instanceof String) {
			String s = (String) arg;
			inta.append(s);
		} else if (arg instanceof Integer) {
			switch ((int) arg) {
			case StateCode.LIANJIECHONGZHI:
				for (int i = 0; i < dtm.getRowCount(); i++)
					dtm.removeRow(i);
				break;
			}
		}
	}
}